const app = getApp();
Page({
  data: {
    roomName: '', // 房间名称
    userName: '', // 用户名称
    pureAudio: false,
    tapTime: '', // 防止两次点击操作间隔太快
    roomPassWord:'',
    evenWheatArray: [
      {name:'管理员',value:0,checked:true},
      {name:'所有人',value:1,checked:false},
    ],
    connectStatus:0,  //默认管理员才有连麦权限
  },
  bindRoomName: function (e) {
    let self = this;
    self.setData({
      roomName: e.detail.value,
    });
  },
  bindRoomPassWord (e) {
		this.setData({
      roomPassWord: e.detail.value,
		});
	},
  bindConnectStatus(e){
    this.setData({
      connectStatus: Number(e.detail.value),
    });
  },
  tapVideo: function () {
    let self = this;
    self.setData({
      pureAudio: false,
    });
  },
  tapAudio: function () {
    let self = this;
    self.setData({
      pureAudio: true,
    });
  },
  create() {
    let self = this;
    let nickName = wx.getStorageSync('wsUserInfo') && wx.getStorageSync('wsUserInfo').nickName;
    let nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    if (!self.data.roomName) {
      wx.showModal({
        title: '提示',
        content: '房间名称不能为空',
        showCancel: false,
      });
      return;
    }
    if (/[<>*{}()^%$#@!~&= ]/.test(self.data.roomName)) {
      wx.showModal({
        title: '提示',
        content: '名称不能为空或包含特殊字符',
        showCancel: false,
      });
      return;
    }
    if (self.data.roomPassWord.length > 11) {
      wx.showModal({
        title: '提示',
        content: '房间密码不能超过11位数字',
        showCancel: false,
      });
      return;
    }
    
    app.globalData.roomPassWord = self.data.roomPassWord;  
    app.globalData.connectStatus = self.data.connectStatus;  
    const url = `../room/room?type=create&roomName=${self.data.roomName}&userName=${nickName}&pureAudio=${self.data.pureAudio}`;
    wx.redirectTo({
      url: url,
    });
    wx.showToast({
      title: '进入房间',
      icon: 'success',
      duration: 1000,
    });
    self.setData({
      tapTime: nowTime,
    });
  },
});
